package com.udp.nml;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.example.udp.MemoryMonitor;
import org.apache.mina.example.udp.MemoryMonitorHandler;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;

public class MonitorService 
{
	private static final int PORT = 44555;
	
	public static void main(String[] args)
	{
		MonitorService monitorService = new MonitorService();
		monitorService.setup();
	}
	
	public void setup()
	{
	
		try 
		{
			
			MemoryMonitor memoryMonitor = new MemoryMonitor();
			
			NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
			
			acceptor.setHandler(new MemoryMonitorHandler(memoryMonitor));
	
			DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
			
			chain.addLast("logger", new LoggingFilter());
	
			DatagramSessionConfig dcfg = acceptor.getSessionConfig();
			dcfg.setReuseAddress(true);
			acceptor.bind(new InetSocketAddress(PORT));
		
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
